import gzip
import json
import pickle

import redis
# 操作redis
# https://www.cnblogs.com/goldsunshine/p/15345710.html
import redis
import re
# MySql配置信息
from dotenv import dotenv_values

config = dotenv_values('.flaskenv')
REDIS_HOST = config.get('REDIS_HOST') or '124.221.213.179'
REDIS_PORT = config.get('REDIS_PORT') or 6379
CACHE_TYPE = config.get('CACHE_TYPE') or 'redis'
CACHE_REDIS_PASSWORD = 'lsz&&ZSR666'


def RedisUtils():
    try:
        # host is the redis host,the redis server and client are required to open, and the redis default port is 6379
        devpool = redis.ConnectionPool(host='localhost', password='', port=6379, db=0,decode_responses=True)
        # pool = redis.ConnectionPool()
        propool = redis.ConnectionPool(host=REDIS_HOST, password=CACHE_REDIS_PASSWORD, port=REDIS_PORT, db=0,decode_responses=True)

        print("connected success.")
    except:
        print("could not connect to redis.")
    dev_r = redis.Redis(connection_pool=devpool)
    pro_r = redis.Redis(connection_pool=propool)
    # return dev_r
    return pro_r

# def RedisUtils():
#     try:
#         # host is the redis host,the redis server and client are required to open, and the redis default port is 6379
#         pool = redis.ConnectionPool(host=REDIS_HOST, password=CACHE_REDIS_PASSWORD, port=REDIS_PORT, db=0,
#                                     decode_responses=True)
#         # pool = redis.ConnectionPool()
#         print("connected success.")
#     except:
#         print("could not connect to redis.")
#     r = redis.Redis(connection_pool=pool)
#     return r


def getttl(str):
    r = RedisUtils()
    key = 'flask_cache_' + str
    TTL = r.ttl(key)
    print(TTL)
    # 关闭连接
    r.connection_pool.disconnect()
    # r.connection_pool.disconnect()
    return TTL


def getkeys(openid):
    r = RedisUtils()
    list = r.keys()
    reslist = []
    for item in list:
        if openid in item:
            reslist.append(item[12:])
    print(reslist)
    return reslist


def getcontent(str):
    r = RedisUtils()
    content = r.get(str)
    print(content)
    # print(gzip.decompress(content).decode('utf-8'))
    return content


if __name__ == '__main__':
    # print(r.ttl('flask_cache_cartslist'))
    # r.ttl(name)
    # getkeys()
    # getttl('8
    # 7cb0b7a-ca0f-49c0-8b6c-d12839b30585ocmg85A_-Bf-Gg_qkXthHHAXkqww')
    getkeys('token')
